package com.ncloud.crm.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.crm.entity.BsCust;



@Mapper
public interface BsCustMapper {
	@Insert("insert into bs_cust(cust_id, company_code, cust_name, cust_state, industry,contact_email, "
			+ "create_time, create_user, update_time, update_user, remark,contact_name,contact_phone)"
			+ " values(#{custId},#{companyCode},#{custName},#{custState},#{industry},#{contactEmail},"
			+ "#{createTime},#{createUser},#{updateTime},#{updateUser},#{remark},#{contactName},#{contactPhone})")
	int insertBsCust(BsCust cust);

	@Update("update bs_cust set cust_name=#{custName},cust_state=#{custState},industry=#{industry},update_time=#{updateTime},"
			+ "update_user=#{updateUser},remark=#{remark},contact_name=#{contactName},"
			+ "contact_phone=#{contactPhone}contact_email=#{contactEmail} where cust_id=#{custId}")
	int updateBsCust(BsCust cust);

	@Select("select * from bs_cust where company_code=#{companyCode} and cust_state='1'")
	@Results({ @Result( column = "cust_id", property = "custId"),
			@Result(column = "company_code", property = "companyCode"),
			@Result(column = "cust_name", property = "custName"),
			@Result(column = "cust_state", property = "custState"),
			@Result(column = "create_time", property = "createTime"),
			@Result( column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),
			@Result( column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark"),
			@Result(column = "industry", property = "industry"),@Result(column = "contact_email", property = "contactEmail"),
			@Result(column = "contact_name", property = "contactName"),@Result(column = "contact_phone", property = "contactPhone")
	})
	BsCust getByCompanyCode(String companyCode);

	// 查询项目对应客户
	@Select(value = { " select a.* from bs_cust a left join bs_project b on a.cust_id = b.cust_id "
			+ "where b.project_id = #{0} and b.manager = #{1} " })
	@Results({ @Result( column = "cust_id", property = "custId"),
			@Result(column = "company_code", property = "companyCode"),
			@Result(column = "cust_name", property = "custName"),
			@Result(column = "cust_state", property = "custState"), @Result(column = "industry", property = "industry"),
			@Result(column = "create_time", property = "createTime"),
			@Result( column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),@Result(column = "contact_email", property = "contactEmail"),
			@Result( column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark"),
			@Result(column = "contact_name", property = "contactName"),@Result(column = "contact_phone", property = "contactPhone")
	})
	public BsCust getBsCust(String projectId, String manager);

	// 通过渠道登陆工号查找工号关联客户信息（bs_cust、bs_channel_cust）
	@Select(value = {
			"select a.* from bs_cust a left join bs_channel_cust b on a.cust_id = b.cust_id where b.user_id = #{0}" })
	@Results({ @Result( column = "cust_id", property = "custId"),
			@Result(column = "company_code", property = "companyCode"),
			@Result(column = "cust_name", property = "custName"),
			@Result(column = "cust_state", property = "custState"),
			@Result(column = "create_time", property = "createTime"),
			@Result( column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),
			@Result( column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark"),
			@Result(column = "industry", property = "industry"),@Result(column = "contact_email", property = "contactEmail"),
			@Result(column = "contact_name", property = "contactName"),@Result(column = "contact_phone", property = "contactPhone")
	})
	public List<BsCust> getBsChannelCust(String userID);

	@Insert("insert into bs_channel_cust(user_id,cust_id,create_time,create_user,remark) values(#{userId},#{custId},now(),#{userId},null)")
	int createChnlCustRel(@Param("custId")Long custId, @Param("userId")Long userId);

	@Select(value = {"select a.* from bs_cust a,bs_channel_cust b where b.cust_id = #{custId} and b.user_id = #{userId} and a.cust_id = b.cust_id" })
	@Results({ @Result( column = "cust_id", property = "custId"),
			@Result(column = "company_code", property = "companyCode"),
			@Result(column = "cust_name", property = "custName"),
			@Result(column = "cust_state", property = "custState"),
			@Result(column = "create_time", property = "createTime"),
			@Result( column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),
			@Result( column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark"),
			@Result(column = "industry", property = "industry"),@Result(column = "contact_email", property = "contactEmail"),
			@Result(column = "contact_name", property = "contactName"),@Result(column = "contact_phone", property = "contactPhone")
	})
	BsCust getChnlCustByCustIdAndUserId(@Param("custId")Long custId, @Param("userId")Long userId);
	/*根据客户id查询客户信息*/
	@Select("select * from bs_cust where cust_id=#{custId} and cust_state='1'")
	@Results({ @Result( column = "cust_id", property = "custId"),
			@Result(column = "company_code", property = "companyCode"),
			@Result(column = "cust_name", property = "custName"),
			@Result(column = "cust_state", property = "custState"),
			@Result(column = "create_time", property = "createTime"),
			@Result( column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),
			@Result( column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark"),
			@Result(column = "industry", property = "industry"),@Result(column = "contact_email", property = "contactEmail"),
			@Result(column = "contact_name", property = "contactName"),@Result(column = "contact_phone", property = "contactPhone")
	})
	BsCust getCustInfoById(Long custId);
	
	
	@Select(value = {"select a.* from bs_cust a where cust_state ='1'" })
	@Results({ @Result(column = "cust_id", property = "custId"),
			@Result(column = "company_code", property = "companyCode"),
			@Result(column = "cust_name", property = "custName"),
			@Result(column = "cust_state", property = "custState"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark"),
			@Result(column = "industry", property = "industry"),
			@Result(column = "contact_email", property = "contactEmail"),
			@Result(column = "contact_name", property = "contactName"),
			@Result(column = "contact_phone", property = "contactPhone") })
	public List<BsCust> getBsCustList();
} 